package com.hnzq.cloud.freshsendmessage.controller;


import com.hnzq.cloud.freshsendmessage.param.CheckPayForOrderParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.apache.rocketmq.spring.support.RocketMQHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Moon
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/sendAfterPayForOrderMessage")
@Slf4j
public class SendPayForOrderController {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //同步改库存，改余额，发序列号，放在监听器进行
    //异步发邮件，计算等级，加积分
    @PostMapping("afterPayForOrder")
    public String afterPayForOrder(@RequestBody CheckPayForOrderParam checkPayForOrderParam){

//        //使用雪花算法计算ID
//        Long XueHuaId = new SnowflakeGenerator().next();

        //构建发送的消息
        Message<CheckPayForOrderParam> message =
                MessageBuilder.withPayload(checkPayForOrderParam).
                        //这里要给订单的id,回查的时候要用
                setHeader(RocketMQHeaders.TRANSACTION_ID, checkPayForOrderParam.getOrderId()).
                build();

        //用事务消息方式发送
        rocketMQTemplate.sendMessageInTransaction(
                "afterPayForOrder_topic", message, checkPayForOrderParam);

        log.info("发送成功");

        return "发送成功";
    }

}

